package insert;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

/**
 * Servlet implementation class PoserQuestionServlet
 */
public class PoserQuestionVenteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PoserQuestionVenteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		HttpSession session = request.getSession();
		RequestDispatcher dis;
		
		//Si je ne suis pas connect� alors je suis redirig�
		Integer membre = (Integer)session.getAttribute("user_id");
		if(membre == null){
			dis = request.getRequestDispatcher("/login.jsp");
			dis.forward(request, response);
			return;
		}
		
		int idVente = 0;
		String vente = request.getParameter("idvente");
		
		String commentaire = request.getParameter("desc");
		
		List<String> erreur = new LinkedList<String>();
		
		if(commentaire == null) {
			erreur.add("Pour ajouter un commentaire, il faut entrer un commentaire.");
		}else if(commentaire.isEmpty()){
			erreur.add("Pour ajouter un commentaire, il faut entrer un commentaire.");
		}
		
		if(vente == null){
			dis = request.getRequestDispatcher("/home");
			dis.forward(request, response);
			return;
		}
	
		
		try{
			idVente = Integer.parseInt(vente);
		}catch(NumberFormatException e){
			erreur.add("Le produit doit �tre un nombre");
		}
			
		
		if(!erreur.isEmpty()){ //Donn�es du formulaire incorrectes, renvoie au formulaire
			request.setAttribute("erreur", erreur);
			dis=request.getRequestDispatcher("acheter?action=voir_transaction&vente="+idVente);
			dis.forward(request,response);
		}else{ //Donn�es correctes, envoie de la requ�te � la base de donn�es
			try{
				Connection con;
				Context namingContext;
				PreparedStatement pstmt;
				
				namingContext = new InitialContext();
				DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
				con = dataSource.getConnection();
				
				pstmt = con.prepareStatement("INSERT INTO `message_vente` (`id_vente`, `id_membre`, `message`) VALUES (?, ?, ?)");
				pstmt.setInt(1, idVente);
				pstmt.setInt(2, membre);
				pstmt.setString(3, commentaire);
				
				pstmt.executeUpdate();
				
				response.sendRedirect("acheter?action=voir_transaction&vente="+idVente);
			}catch(NamingException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.sendRedirect("acheter?action=voir_transaction&vente="+idVente);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.sendRedirect("acheter?action=voir_transaction&vente="+idVente);
			}	
		}
	}
}
